<!-- 素材管理 -->
<template>
  <div class="mod-config">

      <div class="funbtn">
        <el-button type="primary" icon="el-icon-plus" v-if="permissions.mp_wxmaterial_add" @click="addOrUpdateHandle('')">素材新增</el-button>
        <el-button type="primary"  @click="articleHandle">文章管理</el-button>
        <el-button type="primary" @click="articleTypeHandle">文章类型管理</el-button>
        <el-button type="primary" @click="keyWordHandle">关键词回复</el-button>
        <!-- <el-button type="primary" @click="assessHandle">推广海报管理</el-button> -->
      </div>

      <el-form :inline="true" v-show="searchShow" ref="searchForm" :model="dataForm" @keyup.enter.native="searchChange()">
        <el-form-item label="素材类型" prop="sendType">
          <el-select v-model="dataForm.sendType" placeholder="请选择">
            <el-option :value="1" label="图文形式"></el-option>
            <el-option :value="2" label="消息形式"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="searchChange">查询</el-button>
          <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading">
          <el-table-column prop="messageType" label="素材类型">
            <template slot-scope="scope">
              <div>
                <span v-if="scope.row.messageType == 1">文本信息</span>
                <span v-if="scope.row.messageType == 2">图文信息</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="messageTheme" label="素材主题"></el-table-column>
          <el-table-column prop="updateTime" label="生成时间"></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">详情</el-button>
              <el-button v-if="permissions.mp_wxmaterial_add" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
              <el-button v-if="permissions.mp_wxmaterial_add" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
              <el-button v-if="permissions.mp_wxmaterial_add" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">发送</el-button>
            </template>
          </el-table-column>

        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>

    <material v-if="materialVisible" ref="materialDialog" @refreshDataList="getDataList"></material>
    <article-type v-if="articleTypeVisible" ref="articleTypeDialog"></article-type>
    <article-form v-if="articleVisible" ref="articleDialog"></article-form>
    <key-word v-if="keyWordVisible" ref="keyWordDialog"></key-word>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { fetchWxMaterialList, delWxMaterialObj } from '@/api/mp/wxmaterial'

  import Material from './components/Material'
  import ArticleType from './components/articleType/index'
  import ArticleForm from './components/article/index'
  import KeyWord from './components/keyword/index'


  export default {
    data() {
      return {
        wxAccountOptions: [],
        dataForm: {
          status: -1
        },
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,

        materialVisible: false,
        articleTypeVisible: false,
        articleVisible: false,
        keyWordVisible: false
      }
    },
    components: {
      Material,
      ArticleType,
      ArticleForm,
      KeyWord
    },
    created() {
      this.getDataList()
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        fetchWxMaterialList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        })).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.materialVisible = true
        this.$nextTick(() => {
          this.$refs.materialDialog.init(id)
        })
      },
      // 文章管理
      articleHandle(){
        this.articleVisible = true
        this.$nextTick(() => {
          this.$refs.articleDialog.init()
        })
      },
      // 文章类型管理
      articleTypeHandle(){
        this.articleTypeVisible = true
        this.$nextTick(() => {
          this.$refs.articleTypeDialog.init()
        })
      },
      // 关键词管理
      keyWordHandle() {
        this.keyWordVisible = true
        this.$nextTick(() => {
          this.$refs.keyWordDialog.init()
        })
      },

      // 消息模板管理
      messageTemplateHandle() {
        this.messageTemplateVisible = true
        this.$nextTick(() => {
          this.$refs.messageTemplate.init()
        })
      },

      // 评价管理
      assessHandle() {
        this.assessVisible = true
        this.$nextTick(() => {
          this.$refs.assessDialog.init()
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delWxMaterialObj(id)
        }).then(data => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      },
      searchChange() {
        this.pageIndex = 1;
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        for (let key in this.dataForm) {
          this.dataForm[key] = ''
        }
        this.dataForm.status = -1
        this.getDataList(this.page)
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page)
      },
      //显示转换
      sendTypeFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '图文形式'
            break
          case 2:
            return '消息形式'
            break
        }
      },
      sendMethodFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '立即发送'
            break
          case 2:
            return '定时发送'
            break
        }
      },
      sendObjectFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '所有人'
            break
          case 2:
            return '指定对象'
            break
        }
      },
      statusFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '启用'
            break
          case 0:
            return '停用'
            break
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .funbtn {
    display: flex;
    justify-content: flex-end;
    padding: 10px 20px;
    margin-bottom: 15px;
    background-color: #fff;
  }
</style>
